package cn.ssdcc.dkserver.controller;

import cn.ssdcc.dkserver.entity.DkPath;
import cn.ssdcc.dkserver.entity.DkResponse;
import cn.ssdcc.dkserver.entity.ImMsg;
import cn.ssdcc.dkserver.entity.User;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/ImApi")
@Slf4j
public class ImController extends CommonController{

    @RequestMapping(value = "/GetMsg", method = RequestMethod.POST)
    @ResponseBody
    public DkResponse<List<ImMsg>> GetMsg(@RequestBody ImMsg imMsg) {
        List<ImMsg> imMsgs = imMapper.getnewImMsg(imMsg.getTime());
        return new DkResponse<List<ImMsg>>(imMsgs);
    }

    @RequestMapping(value = "/GetMsgHistory", method = RequestMethod.POST)
    @ResponseBody
    public DkResponse<List<ImMsg>> GetMsgHistory(@RequestBody ImMsg imMsg) {
        List<ImMsg> imMsgs = imMapper.getnewImMsgHistory(imMsg.getTime());
        return new DkResponse<List<ImMsg>>(imMsgs);
    }

    @RequestMapping(value = "/SendMsg", method = RequestMethod.POST)
    @ResponseBody
    public DkResponse SendMsg(@RequestBody ImMsg imMsg) {
        User user = userBiz.getUserMsgByDeviceid(imMsg.getUserid());
        imMsg.setUserid(user.getId());
        imMsg.setTime(System.currentTimeMillis()+"");
        imMapper.insertImMsg(imMsg);
        webSocket.sendAllMessage("ImMsg");
        return new DkResponse();
    }

    @RequestMapping("/UpImFile")
    @ResponseBody
    public DkResponse<String> UpImFile(@RequestParam("file") MultipartFile file){
        log.info("文件名："+file.getOriginalFilename());
        String imgpath = userBiz.buildPath("/im/",file.getOriginalFilename());
        log.info(imgpath);
        upLoadBiz.saveMultipartFile(file, DkPath.DataPath+imgpath);
        DkResponse<String> dkResponse = new DkResponse<>();
        dkResponse.setData(imgpath);
        return dkResponse;
    }

}
